/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-05-13 14:21:07
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-05-13 20:20:32
 * @FilePath: /lesson40/3.Thread/Thread.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <ctime>
#include <string>

using namespace std;

typedef void (*callback_t)();

static int num = 1;
class Thread
{
public:
    static void *Routine(void *args)
    {
        Thread *t = static_cast<Thread *>(args);
        t->Entery();
        return nullptr;
    }

public:
    Thread(callback_t cb)
        : tid_(0), tname_(""), starttime_(0), Isrunning_(false), cb_(cb)
    {
    }

    void Run()
    {
        tname_ = "thread-" + to_string(num++);
        starttime_ = time(nullptr);
        Isrunning_ = true;
        pthread_create(&tid_, nullptr, Routine, this);
    }
    void Join()
    {
        pthread_join(tid_, nullptr);
    }
    string Name()
    {
        return tname_;
    }
    uint64_t StartTime()
    {
        return starttime_;
    }
    bool IsRunning()
    {
        return Isrunning_;
    }
    void Entery()
    {
        cb_();
    }
    ~Thread()
    {
    }

private:
    pthread_t tid_;
    string tname_;
    uint64_t starttime_;
    bool Isrunning_;
    callback_t cb_;
};